package com.miquan.minotehelper.utils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.view.View;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by mq on 2015/8/6.
 * --- 流弊 ---
 */
public class ImageUtils {
    /**
     * 截图。如果传入的v == null，则截全屏
     */
    public static Bitmap getBitmap(View view) {
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        return view.getDrawingCache();
    }

    /**
     * 截全屏
     */
    public static Bitmap getScreenBitmap(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        return view.getDrawingCache();
    }

    /**
     * 保存图片为PNG
     */
    public static void savePNG(Bitmap bitmap, File fileToWrite) throws IOException {
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(fileToWrite);
            if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)) {
                out.flush();
            }
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
}
